package com.dnzx.controller.admin;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.dnzx.framework.page.PageResult;
import org.dnzx.framework.result.ResultUtils;
import org.dnzx.framework.result.domain.Result;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dnzx.model.CategoryBO;
import com.dnzx.service.ICategoryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/category")
@Api(value = "/category",description="商品分类",produces=MediaType.APPLICATION_JSON_VALUE)
public class CategoryController {
	
	@Resource
	private ICategoryService categoryService;
	
	/**
	 * 新增
	 * @param categoryBO
	 * @return
	 */
	@PostMapping(value = "/save")
    @ApiOperation(value = "/save",notes = "新增:userId、type:充值-1；消费-2、amt（消费传负数）",httpMethod = "POST",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result save(@ApiParam(value = "实体") @RequestBody @Valid CategoryBO categoryBO) {
		try{
			this.categoryService.save(categoryBO);
	        return ResultUtils.returnSuccess("SUCCESS");
        }catch (Exception e){
            return ResultUtils.returnError("ERROR >>> " + e.getMessage());
        }
    }
	
	/**
	 * 查看
	 * @param id
	 * @return
	 */
	@GetMapping("/get/{id}")
	@ApiOperation(value = "/get/{id}",notes = "查看",httpMethod = "GET",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result get(@ApiParam(value = "id") @PathVariable("id") Long id) {
		try {
			CategoryBO categoryBO = this.categoryService.get(id);
			return ResultUtils.returnSuccess("SUCCESS", categoryBO);
		}catch(Exception e) {
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete/{id}")
	@ApiOperation(value = "/delete/{id}",notes = "删除",httpMethod = "DELETE",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result delete(@ApiParam(value = "id") @PathVariable("id") Long id){
		try {
			this.categoryService.delete(id);
			return ResultUtils.returnSuccess("SUCCESS");
		}catch(Exception e) {
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}

	/**
	 * 修改
	 * @param categoryBO
	 * @return
	 */
	@PutMapping(value = "/update")
	@ApiOperation(value = "/update",notes = "修改:id、name、sex、mobile、idCard",httpMethod = "PUT",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result update(@ApiParam(value = "实体") @RequestBody CategoryBO categoryBO) {
		try {
			this.categoryService.update(categoryBO);
			return ResultUtils.returnSuccess("SUCCESS");
		}catch(Exception e) {
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}


	/**
	 * 查询 - 分页
	 * @param categoryBO
	 * @return
	 */
	@PostMapping("/queryPage")
	@ApiOperation(value="/queryPage",notes="查询 - 分页：参数：type",httpMethod="POST",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result queryPage(@ApiParam(value = "实体") @RequestBody CategoryBO categoryBO) {
		try {
			PageResult<List<CategoryBO>> pageResult = new PageResult<List<CategoryBO>>();
			pageResult = this.categoryService.queryPage(categoryBO, pageResult);
			return ResultUtils.returnSuccess("SUCCESS", pageResult);
		}catch(Exception e) {
			e.printStackTrace();
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
	/**
	 * 查询 - 分页
	 * @param categoryBO
	 * @return
	 */
	@PostMapping("/queryList")
	@ApiOperation(value="/queryList",notes="查询 -不 分页：参数：pid",httpMethod="POST",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result queryList(@ApiParam(value = "实体") @RequestBody CategoryBO categoryBO) {
		try {
			List<CategoryBO> categoryBOList = this.categoryService.queryList(categoryBO);
			if (categoryBOList != null && categoryBOList.size() > 0) {
				for (CategoryBO categoryBOTemp : categoryBOList) {
					CategoryBO categoryBOQuery = new CategoryBO();
					categoryBOQuery.setPid(categoryBOTemp.getId());
					List<CategoryBO> subCategoryBOList = this.categoryService.queryList(categoryBOQuery);
					if (subCategoryBOList != null && subCategoryBOList.size() > 0) {
						categoryBOTemp.setSubCategoryBOList(subCategoryBOList);
					}
				}
			}
			return ResultUtils.returnSuccess("SUCCESS", categoryBOList);
		}catch(Exception e) {
			e.printStackTrace();
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
}
